﻿/*
 * Created by SharpDevelop.
 * User: Intel
 * Date: 20.12.2013
 * Time: 15:55
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
namespace Region.Utils
{
  /// <summary>
  /// Description of Convert.
  /// </summary>
  public class Convert
  {
        enum TypeChar{Null,Num,Space,Point,Other};
        static TypeChar getTypeChar(Char C)
        {
            if (C >= '0' && C <= '9') return TypeChar.Num;
            else if (C == ' ') return TypeChar.Space;
            else if (C == '.') return TypeChar.Point;
            else if (C == ',') return TypeChar.Point;
            else return TypeChar.Other;
        }
        /// <summary>
        /// см. тест Test1
        /// </summary>
        /// <param name="TimeString"></param>
        /// <param name="TimeValue"></param>
        /// <returns></returns>
        static public bool readTime(String TimeString,out Int32 TimeValue)
        {
          TimeValue=0;
         //
          List<String> lit = new List<String>();
          TypeChar oldType = TypeChar.Null;
          String S="";
          for(int i=0;i<TimeString.Length;i++)
          {
            Char C = TimeString[i];
            TypeChar newType = getTypeChar(C);
            if (oldType!=newType)
            {
              oldType = newType;
              if (S.Length>0) 
                lit.Add(S);
              S=""+C;
            }
            else
              S+=C;
          }
          if (S.Length>0) lit.Add(S);
          // Объединить дробные числа
          if (lit.Count>=3)
            for(Int32 i=0;i<lit.Count-2;i++)
              if (getTypeChar(lit[i][0])==TypeChar.Num)
                if (getTypeChar(lit[i+1][0])==TypeChar.Point && lit[i+1].Length==1)
                  if (getTypeChar(lit[i+2][0])==TypeChar.Num)
          {
            lit[i]=lit[i]+lit[i+1]+lit[i+2];
            lit.RemoveAt(i+2);
            lit.RemoveAt(i+1);
          }
          
          // Удалить лишние пробелы
          for(Int32 i=0;i<lit.Count;)
            if (getTypeChar(lit[i][0])==TypeChar.Space)
              lit.RemoveAt(i);
            else
              i++;
          // Посчитать колличество цифр
          Int32 NumValue=0;
          for(Int32 i=0;i<lit.Count;i++)
            if (getTypeChar(lit[i][0])==TypeChar.Num)
              NumValue++;
                
              
          if (NumValue==1)
          {
            Boolean H1 = false, H2=false, M1=false, M2=false;
            Int32 pos=0;
            for(;pos<lit.Count && getTypeChar(lit[pos][0])!=TypeChar.Num;pos++)
            {
              if (lit[pos].Contains("м")||lit[pos].Contains("М")||lit[pos].Contains("m")||lit[pos].Contains("M")) M1=true;
              if (lit[pos].Contains("ч")||lit[pos].Contains("Ч")||lit[pos].Contains("h")||lit[pos].Contains("H")) H1=true;
            }
            String Value = lit[pos];
            Value = Value.Replace(',','.');
            Boolean FloatValue=false;
            if (Value.Contains(".")) FloatValue=true;
            for(pos=pos+1;pos<lit.Count && getTypeChar(lit[pos][0])!=TypeChar.Num;pos++)
            {
              if (lit[pos].Contains("м")||lit[pos].Contains("М")||lit[pos].Contains("m")||lit[pos].Contains("M")) M2=true;
              if (lit[pos].Contains("ч")||lit[pos].Contains("Ч")||lit[pos].Contains("h")||lit[pos].Contains("H")) H2=true;
            }
            Int32 H=0,M=0;
            if (FloatValue) 
            {
              float V=0;
              if (!float.TryParse(Value,System.Globalization.NumberStyles.Number,System.Globalization.CultureInfo.CreateSpecificCulture("en-UK"),out V)) return false;
              H = (Int32)V;
              V=V-H;
              M=(Int32)(V*60);
              TimeValue = M + H * 60;
              return true;
            }
            else
            {
              Int32 T=0;
              if (H2) T=0;
              else if (M2) T=1;
              else if (H1) T=0;
              else if (M1) T=1;
              if (T==0)
              {
                if (!Int32.TryParse(Value,out H)) return false;
                TimeValue = H * 60;
                return true;
              }
              else
              {
                if (!Int32.TryParse(Value,out M)) return false;
                TimeValue = M;
                return true;
              }
            }
            return false;
          }
          else if (NumValue==2)
          {
            Boolean H1 = false, H2=false,H3=false, M1=false, M2=false, M3=false;
            Int32 pos=0;
            for(;pos<lit.Count && getTypeChar(lit[pos][0])!=TypeChar.Num;pos++)
            {
              if (lit[pos].Contains("м")||lit[pos].Contains("М")||lit[pos].Contains("m")||lit[pos].Contains("M")) M1=true;
              if (lit[pos].Contains("ч")||lit[pos].Contains("Ч")||lit[pos].Contains("h")||lit[pos].Contains("H")) H1=true;
            } 
            String Value1 = lit[pos];            
            for(pos=pos+1;pos<lit.Count && getTypeChar(lit[pos][0])!=TypeChar.Num;pos++)
            {
              if (lit[pos].Contains("м")||lit[pos].Contains("М")||lit[pos].Contains("m")||lit[pos].Contains("M")) M2=true;
              if (lit[pos].Contains("ч")||lit[pos].Contains("Ч")||lit[pos].Contains("h")||lit[pos].Contains("H")) H2=true;
            } 
            String Value2 = lit[pos];
            for(pos=pos+1;pos<lit.Count && getTypeChar(lit[pos][0])!=TypeChar.Num;pos++)
            {
              if (lit[pos].Contains("м")||lit[pos].Contains("М")||lit[pos].Contains("m")||lit[pos].Contains("M")) M3=true;
              if (lit[pos].Contains("ч")||lit[pos].Contains("Ч")||lit[pos].Contains("h")||lit[pos].Contains("H")) H3=true;
            } 
            Int32 H=0,M=0;
            if (!Int32.TryParse(Value1,out H)) return false;
            if (!Int32.TryParse(Value2,out M)) return false;
            TimeValue = H * 60 + M;
            return true;
          }
          return false;
        }
  }
}
